<?php
require_once 'SQLHelper.class.php';

//该类是一个业务逻辑处理类，主要完成blog表的操作
class BlogService{
		
	//这是一个显示最新的文章标题的函数
	//$countTitle
	function getLatestBlogTitle($countTitle){
			
		$sql = "select title,id from blog order by time desc limit 0,$countTitle";
		$sqlHelper = new SQLHelper();
		$arr = $sqlHelper->execute_dql($sql);
		//关闭数据库连接
		$sqlHelper->close_connect();

		for($i=0;$i<count($arr);$i++){
			$row = $arr[$i];
                	$id = $row['id'];
                	$all[]= "<li><a href='blogPage.php?id=$id'>{$row['title']}</a></li>";
        	}	
		
		//处理当没有任何博文时的情况
		if(!empty($all)){
			$latest = implode($all);
		}else{
			$latest = "没有任何文章";
		}
		return $latest;
			
	}

	//获取分页信息(包括考虑是否按目录(即导航栏)分类)
	function getPager($pager){
		//创建一个sqlHelper对象实例
		$sqlHelper = new SQLHelper();


		if(empty($_GET['list'])){

			//!limit后面必须要有空格建
			$sql1 = "select * from blog order by time desc limit ".($pager->pageNow-1)*$pager->pageSize.",$pager->pageSize";
		}else {
			$cid = $_GET['list'];
        		$sql1 = "select id,title,content from blog where cid=$cid order by time desc limit ".($pager->pageNow-1)*$pager->pageSize.",$pager->pageSize";

		}
		
		//需要查询显示总行数$rowCount
		$sql2 = "select count(id) from blog";
		
		$arr = $sqlHelper->execute_dql_pager($sql1,$sql2,$pager);
	}

	//浏览博文
	function viewBlog($id){
		$sqlHelper = new SQLHelper();
		$sql = "select * from blog where id=$id";
		$arr=$sqlHelper->execute_dql($sql);
		for($i=0;$i<count($arr);$i++){
			$row = $arr[$i];
			$arr = "<div id='title'><h2>{$row['title']}</h2></div>
				<div id='content'><p>{$row['content']}</p></div>";
		}
	
		//关闭数据库连接
		$sqlHelper->close_connect();	
		return $arr;
	}

	//删除博文，我觉的可以把trashe做成可以回收站好点
	function trashedBlog($id){
		$sqlHelper = new SQLHelper();
		
		$sql = "delete from blog where id=$id";
		$res = $sqlHelper->execute_dml($sql);
		$sqlHelper->close_connect();	
	}

	//添加博文
	function addBlog($title,$content,$cid){
		$sqlHelper = new SQLHelper();
		
		$sql = "insert into blog(title,content,cid) value('$title','$content',$cid)";
		$res = $sqlHelper->execute_dml($sql);
		$sqlHelper->close_connect();
	}

}
?>

